package com.rzico.report.controller;


import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.report.entity.ChartDataVo;
import com.rzico.report.entity.SaleRoomVo;
import com.rzico.report.service.SaleRoomReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 销售数据统计
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-15
 */
@Api(description = "销售数据统计")
@RestController
@RequestMapping("/report/saleRoomReport")
public class SaleRoomReportController {

    @Autowired
    private SaleRoomReportService saleRoomReportService;

    @Autowired
    private EnterpriseService enterpriseService;

    @ApiOperation("经营统计头部")
    @GetMapping("/showSaleHead")
    public CommResult<SaleRoomVo> head() {
        Map<String,Object> params = new HashMap();

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        //endDate为今天日期
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        //startDate为昨天日期
        Date yesterday  = c.getTime();
        Date startDate = DateUtils.truncate(yesterday, Calendar.DATE);
        Date endDate = DateUtils.addMilliseconds(DateUtils.addDays(startDate,2),-1);
        params.put("startDate", startDate);
        params.put("endDate", endDate);

        List<SaleRoomVo> saleRoomVos = saleRoomReportService.showSaleAndFee(params);

        //为空的情况下返回空
        if (saleRoomVos == null || saleRoomVos.size() == 0 ) {
            return CommResult.success(new SaleRoomVo());
        }
        //如果不为空,且昨天或者今天其中一天有数据
        SaleRoomVo saleRoomVoToday = null;
        SaleRoomVo saleRoomVoYestoday = null;
        if (saleRoomVos != null && saleRoomVos.size() == 1) {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
            String compareDate = fmt.format(saleRoomVos.get(0).getOrderDate());
            String compareToday = fmt.format(today);
            String compareYestoday = fmt.format(yesterday);
            if (compareDate.equals(compareToday)) {
                saleRoomVoToday = saleRoomReportService.showOrderStatus(params);
                if (saleRoomVoToday.getValid_order_today() > 0){
                    saleRoomVoToday.setUnit_price(saleRoomVos.get(saleRoomVos.size()-1).getAmount_payable().divide(BigDecimal.valueOf(saleRoomVoToday.getValid_order_today()),2,BigDecimal.ROUND_DOWN));
                }
                saleRoomVoToday.setAmount_payable(saleRoomVos.get(0).getAmount_payable());
                saleRoomVoToday.setFee(saleRoomVos.get(0).getFee().abs());
            } else if(compareDate.equals(compareYestoday)) {
                saleRoomVoToday = new SaleRoomVo();
                params.put("endDate", org.apache.commons.lang3.time.DateUtils.truncate(startDate, Calendar.DATE));
                saleRoomVoYestoday = saleRoomReportService.showOrderStatus(params);
                if (saleRoomVoYestoday.getValid_order_today() > 0 || saleRoomVoYestoday.getValid_order_today() > 0){
                    double v = new BigDecimal((float) saleRoomVoYestoday.getInvalid_order_today() / (saleRoomVoYestoday.getInvalid_order_today() + saleRoomVoYestoday.getValid_order_today())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    saleRoomVoToday.setCancel_rate(v);
                }
                saleRoomVoToday.setAmount_payable_yestoday(saleRoomVos.get(0).getAmount_payable());
                saleRoomVoToday.setFee_yestoday(saleRoomVos.get(0).getFee().abs());
            }
        } else if (saleRoomVos != null && saleRoomVos.size() == 2) {
            saleRoomVoToday = saleRoomReportService.showOrderStatus(params);
            if (saleRoomVoToday.getValid_order_today() > 0){
                saleRoomVoToday.setUnit_price(saleRoomVos.get(saleRoomVos.size()-1).getAmount_payable().divide(BigDecimal.valueOf(saleRoomVoToday.getValid_order_today()),2,BigDecimal.ROUND_DOWN));
            }
            params.put("endDate", org.apache.commons.lang3.time.DateUtils.truncate(startDate, Calendar.DATE));
            saleRoomVoYestoday = saleRoomReportService.showOrderStatus(params);
            if (saleRoomVoYestoday.getValid_order_today() > 0 || saleRoomVoYestoday.getValid_order_today() > 0){
                double v = new BigDecimal((float) saleRoomVoYestoday.getInvalid_order_today() / (saleRoomVoYestoday.getInvalid_order_today() + saleRoomVoYestoday.getValid_order_today())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                saleRoomVoToday.setCancel_rate(v);
            }

            saleRoomVoToday.setAmount_payable(saleRoomVos.get(1).getAmount_payable());
            saleRoomVoToday.setFee(saleRoomVos.get(1).getFee().abs());

            saleRoomVoToday.setAmount_payable_yestoday(saleRoomVos.get(0).getAmount_payable());
            saleRoomVoToday.setFee_yestoday(saleRoomVos.get(0).getFee().abs());
        }
        if (saleRoomVoToday == null) {
            saleRoomVoToday = new SaleRoomVo();
        }
        //saleRoomVoToday.setList(saleRoomVos);
        return CommResult.success(saleRoomVoToday);
    }

    @ApiOperation("图表统计数据获取")
    @GetMapping("/showChartData")
    public CommResult<ChartDataVo> showChart() {
        Map<String,Object> params = new HashMap();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        List<ChartDataVo> list = saleRoomReportService.showChartData(params);
        return CommResult.success(list);
    }
}
